package com.magic.zhixin.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

public class SercerityUtils {

//    public static String sign(Map param, String key) {
//        return null;
//    }
    public static String sign(String param0, String key) {
        String[] param = param0.split("\\|");
        StringBuffer sb = new StringBuffer();
        int len = param.length;
        for (int i = 0;i<len ;i++) {
            if (!StringUtils.isEmpty(param[i])) {
                sb.append(param[i]);
                sb.append("|");
            }
        }
        sb.append(key);
        return DigestUtils.md5DigestAsHex(sb.toString().getBytes()).toLowerCase();
    }

    public static String sign2(Map data, String key){
        Set<String> keySet = data.keySet();
        String[] keyArray = (String[])keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        String[] var6 = keyArray;
        int var7 = keyArray.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String k = var6[var8];
            if (((String)data.get(k)).trim().length() > 0) {
                sb.append(((String)data.get(k)).trim()).append("|");
            }
        }

        sb.append(key);
        return DigestUtils.md5DigestAsHex(sb.toString().getBytes()).toLowerCase();
    }



    public static String getKey(Map<String, Object> remoteMap,String token) throws UnsupportedEncodingException {
        String key = "";
        if (null != remoteMap.get("goodsname")) {
            key += remoteMap.get("goodsname");
        }
        if (null != remoteMap.get("istype")) {
            key += remoteMap.get("istype");
        }
        if (null != remoteMap.get("notify_url")) {
            key += remoteMap.get("notify_url");
        }
        if (null != remoteMap.get("orderid")) {
            key += remoteMap.get("orderid");
        }
        if (null != remoteMap.get("orderuid")) {
            key += remoteMap.get("orderuid");
        }
        if (null != remoteMap.get("price")) {
            key += remoteMap.get("price");
        }
        if (null != remoteMap.get("return_url")) {
            key += remoteMap.get("return_url");
        }
        key += token;
        if (null != remoteMap.get("uid")) {
            key += remoteMap.get("uid");
        }
//        return MD5Util.encryption(key);
        return null;
    }

}
